module view {
    //仓库
    export class StoreView extends eui.Component {
        closeBtn: eui.Button
        storeGroup: eui.Group
        bagGroup: eui.Group
        bagScroller: eui.Scroller
        storeScroller: eui.Scroller
        itemNameLabel: eui.Label
        selectBtn: eui.Button
        bgBtn: eui.Button
        public constructor() {
            super()
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = "resource/skin/StoreSkin.exml"
        }

        protected onComplete(): void {
            this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.showStore()
            this.showBag()
        }

        private close(): void {
            this.stage.removeChild(this)
        }

        private showStore(): void {
            this.storeGroup.removeChildren()
            var j = 0
            for(var i in gplayer.store.items) {
                var item = gplayer.store.items[i]
                if(!item) {
                    continue
                }
                var btn: eui.Button = new eui.Button()
                btn.skinName = "resource/control/Button3Skin.exml"
                var width = btn.width
                var height = btn.height
                var y = Math.floor(j / 2) * (height + 20)
                var x = 0
                if(j % 2 === 0) {
                    x = 540 / 4 - width / 2
                } else {
                    x = 540 * 3 / 4 - width / 2
                }
                btn.x = x
                btn.y = y
                btn['originalPos'] = {
                    x: x,
                    y: y
                }
                j += 1
                this.storeGroup.addChild(btn)
                var itemId = item.id
                var stack = item.stack
                btn.label = templateName[itemId] + 'X' + stack
                btn['value'] = itemId
                btn['packageType'] = gplayer.store.type
                btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.showItemInfo,this)
                btn.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.touchBegin,this)
            }
        }

        private showBag(): void {
            this.bagGroup.removeChildren()
            var j = 0
            for(var i in gplayer.bag.items) {
                var item = gplayer.bag.items[i]
                if(!item) {
                    continue
                }
                var btn: eui.Button = new eui.Button()
                btn.skinName = "resource/control/Button3Skin.exml"
                var width = btn.width
                var height = btn.height
                var y = Math.floor(j / 2) * (height + 20)
                var x = 0
                if(j % 2 === 0) {
                    x = 540 / 4 - width / 2
                } else {
                    x = 540 * 3 / 4 - width / 2
                }
                btn.x = x
                btn.y = y
                btn['originalPos'] = {
                    x:x,
                    y:y
                }
                j += 1
                this.bagGroup.addChild(btn)
                var itemId = item.id
                var stack = item.stack
                btn.label = templateName[itemId] + 'X' + stack
                btn['value'] = itemId
                btn['packageType'] = gplayer.bag.type
                btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.showItemInfo,this)
                btn.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.touchBegin,this)
                //btn.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchMove,this)
                //btn.addEventListener(egret.TouchEvent.TOUCH_END,this.touchEng,this)
                //btn.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE,this.touchEng,this)
            }
        }

        private touchBegin(evt: egret.TouchEvent) {
            var target = evt.target
            target['flag'] = true
            target['startPos'] = {
                x: evt.localX,
                y: evt.localY
            }
            target.visible = false
            //target.parent.swapChildrenWithEnd(target)
            var bgBtn: eui.Button = new eui.Button()
            bgBtn.width = this.stage.width
            bgBtn.height = this.stage.height
            this.addChild(bgBtn)
            bgBtn.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchMove,this)
                this.bgBtn = bgBtn
            var btn: eui.Button = new eui.Button()
            btn.skinName = "resource/control/Button3Skin.exml"
            btn.label = target.label
            var originalPos = target['originalPos']
            if(target['packageType'] === gplayer.bag.type) {
                btn.y = originalPos.y + this.bagScroller.y
                btn.x = originalPos.x + this.bagScroller.x
            }else{
                btn.y = originalPos.y + this.storeScroller.y
                btn.x = originalPos.x + this.storeScroller.x
            }
            this.addChild(btn)
            btn.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchMove,this)
            btn.addEventListener(egret.TouchEvent.TOUCH_END,this.touchEng,this)
            btn.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE,this.touchEng,this)         
            btn['originalBtn'] = target
            btn['startPos'] = {
                x: evt.localX,
                y: evt.localY
            }
            this.selectBtn = btn
            this.selectBtn['flag'] = false
            //this.stage.addEventListener(egret.TouchEvent.TOUCH_END,this.touchEng,this)
            //this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchEng,this)   
            var target = evt.target
            var itemId = target.value
            var itemConfig = totalConfig[itemId]
            this.itemNameLabel.text = itemConfig.name
        }

        private touchMove(evt: egret.TouchEvent) {
            var target = evt.target
            var startPos = this.selectBtn['startPos']
            var relativeX = evt.stageX - startPos.x
            var relativeY = evt.stageY - startPos.y
            this.selectBtn.x = relativeX
            this.selectBtn.y = relativeY
        }

        
        private touchEng(evt: egret.TouchEvent) {                    
            var target = this.selectBtn
            //this.stage.removeEventListener(egret.TouchEvent.TOUCH_END,this.touchEng,this);
            if(target['flag'] === true){
                return
            }
            target['flag'] = false
            var btn = target['originalBtn']
            if(btn['packageType'] === gplayer.bag.type){
                if(target.y > this.storeScroller.y && target.y < this.storeScroller.y + this.storeScroller.height){
                    var itemId = btn['value']
                    var itemComponent = gplayer.bag.getItemComponent(itemId)
                    gplayer.store.addItem(itemComponent)
                    gplayer.bag.removeItemById(itemId)
                    this.showStore()
                    this.showBag()
                } else {
                    btn.visible = true
                }    
            } else if(btn['packageType'] === gplayer.store.type){
                if(target.y > this.bagScroller.y && target.y < this.bagScroller.y + this.bagScroller.height) {
                    var itemId = btn['value']
                    var itemComponent = gplayer.store.getItemComponent(itemId)
                    gplayer.bag.addItem(itemComponent)
                    gplayer.store.removeItemById(itemId)
                    this.showStore()
                    this.showBag()
                } else {
                    btn.visible = true
                }      
            }
            this.removeChild(target)
            this.removeChild(this.bgBtn)
        }

        private showItemInfo(evt: egret.TouchEvent): void {
            var target = evt.target
            var itemId = target.value
            var itemConfig = totalConfig[itemId]
            this.itemNameLabel.text = itemConfig.name
        }
    }
}
